<template>
  <div class="app-container">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="title" label="标题">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入姓名"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="flex-x-between mb-10px">
          <div>
            <el-button type="success" icon="plus" @click="handleOpenDialog(null)">新增</el-button>
          </div>
        </div>
        <div class="table-card-body">
          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            @handle-query="handleChangePage"
          >
            <template #status="{ row }">
              <el-tag :type="row.status === ENABLE_STATUS_ENUM.ENABLED.value ? 'success' : 'info'">
                {{ getLabelByValue(ENABLE_STATUS_ENUM, row.status) }}
              </el-tag>
            </template>
            <template #linkUrl="{ row, name }">
              <a :href="row.linkUrl" target="_blank">{{ row.linkUrl }}</a>
            </template>
            <template #imageUrl="{ row, name }">
              <el-image
                style="width: 60px; height: 60px"
                :src="row.imageUrl"
                fit="cover"
              ></el-image>
            </template>
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleOpenDialog(row)">
                编辑
              </el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(row.id)">
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
      <banner-form ref="bannerFormRef" @load-data="handleQuery"></banner-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getLabelByValue } from "@/utils/enums";
import BasePageTable from "@/components/BasePageTable/index.vue";
import { ENABLE_STATUS_ENUM } from "@/enums/brand/BrandEnum";
import BannerForm from "@/views/system/banner/components/banner-form.vue";
import bannerApi, { BannerPageQuery, SysBannerVO } from "@/api/mall/banner-api";

const loading = ref(false);
const bannerFormRef = ref();
const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<BannerPageQuery>({
  title: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<SysBannerVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "title",
    label: "标题",
    showOverflowTooltip: true,
  },
  {
    prop: "imageUrl",
    label: "图片",
    showOverflowTooltip: true,
    customSlot: "imageUrl",
    width: 150,
  },
  {
    prop: "linkUrl",
    label: "跳转地址",
    showOverflowTooltip: true,
    customSlot: "linkUrl",
  },
  {
    prop: "remark",
    label: "备注",
    showOverflowTooltip: true,
  },
  {
    prop: "sort",
    label: "排序",
    showOverflowTooltip: true,
    width: 100,
  },
  {
    prop: "status",
    label: "启用/禁用",
    showOverflowTooltip: true,
    width: 100,
    customSlot: "status",
  },
  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);

function handleChangePage({ pageNum, pageSize }) {
  queryParams.pageNum = pageNum;
  queryParams.pageSize = pageSize;
  handleQuery();
}

function search() {
  queryParams.pageNum = 1;
  handleQuery();
}
// 查询
function handleQuery() {
  console.log(12312312312);

  loading.value = true;
  bannerApi
    .getPage(queryParams)
    .then((data) => {
      console.log(data);
      tableData.value = data.data || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

async function handleDelete(id) {
  const res = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });

  if (!res) return;
  loading.value = true;
  try {
    await bannerApi.delete(id);
    handleQuery();
    ElMessage.success("删除成功");
  } catch (e) {
    console.log(e);
  } finally {
    loading.value = false;
  }
}

function handleOpenDialog(row) {
  bannerFormRef.value.showModal(row);
}

onMounted(async () => {
  handleQuery();
});
</script>
